using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Globalization;
using MosziNET.Web.Gallery.Providers;

namespace MosziNET.Web.Gallery.Controls
{
    /// <summary>
    /// The <c>ImageViewCotrol</c> is responsible of showing the specified
    /// image in the gallery.
    /// </summary>
    public class ImageViewControl : CompositeControl
    {
        FolderImage image;

        HyperLink mainImageLink;
        Image mainImage;
        ImageLink previousLink, galleryLink, nextLink;

        #region / Overrides /

        /// <summary>
        /// Creates the child controls in the image view control
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // create a cell that will hold the controls
            TableCell mainCell = new TableCell();

            // create the top navigation bar and the content tables
            mainCell.Controls.Add(CreateTopNavigationBar());
            mainCell.Controls.Add(CreateContent());

            // finally wrap the cell by creating a table and add it to the 
            // control collection
            this.Controls.Add(CreateMainTable(mainCell));
        }

        #endregion / Overrides /

        #region / Public methods /

        /// <summary>
        /// Gets/sets the image that should be shown
        /// </summary>
        public FolderImage FolderImage
        {
            get
            {
                return this.image;
            }
            set
            {
                this.image = value;
                if (this.image != null)
                {
                    mainImageLink.NavigateUrl = HelperMethods.GetImageUrl(
                        this.image, String.Empty, false, false);
                    mainImage.ImageUrl = mainImageLink.NavigateUrl;

                    previousLink.NavigateUrl = GalleryManager.GetImageLinkUrl(
                            GalleryManager.StorageProvider.GetRelatedImage(
                            this.FolderImage, Relation.Previous));
                    galleryLink.NavigateUrl = GalleryManager.GetFolderUrl(this.FolderImage.ParentFolder);
                    nextLink.NavigateUrl = GalleryManager.GetImageLinkUrl(
                            GalleryManager.StorageProvider.GetRelatedImage(
                            this.FolderImage, Relation.Next));
                }
            }
        }

        #endregion / Public methods /

        #region / Control builder methods /

        /// <summary>
        /// Creates the content for the image view control
        /// </summary>
        /// <returns></returns>
        private System.Web.UI.Control CreateContent()
        {
            Table contentTable = new Table();
            if (!String.IsNullOrEmpty(GalleryManager.ImageTableClass))
                contentTable.CssClass = GalleryManager.ImageTableClass;

            TableRow row = new TableRow();

            TableCell imageCell = new TableCell();

            contentTable.Rows.Add(row);
            row.Cells.Add(imageCell);

            mainImageLink = new HyperLink();
            mainImage = new Image();
            mainImageLink.Controls.Add(mainImage);
            imageCell.Controls.Add(mainImageLink);

            // now set the url for the image
            mainImage.Width = new Unit(GalleryManager.PreviewImageWidth);

            return contentTable;
        }

        /// <summary>
        /// Creates the top navigation bar
        /// </summary>
        /// <returns></returns>
        private System.Web.UI.Control CreateTopNavigationBar()
        {
            Table navigationBar = new Table();
            if (!String.IsNullOrEmpty(GalleryManager.NavigationTableClass))
                navigationBar.CssClass = GalleryManager.NavigationTableClass;

            navigationBar.Rows.Add(new TableRow());

            previousLink = new ImageLink("#",
                    GetResourceUrl(Properties.Resources.Image_Previous),
                    Properties.Resources.NavBar_Previous);

            galleryLink = new ImageLink(
                    "#", String.Empty, Properties.Resources.NavBar_GoBack);

            nextLink = new ImageLink("#",
                    GetResourceUrl(Properties.Resources.Image_Next),
                    Properties.Resources.NavBar_Next);

            navigationBar.Rows[0].Cells.Add(WrapImageLink(previousLink));
            navigationBar.Rows[0].Cells.Add(WrapImageLink(galleryLink));
            navigationBar.Rows[0].Cells.Add(WrapImageLink(nextLink));

            return navigationBar;
        }

        /// <summary>
        /// Wraps the specified image link into a table cell
        /// </summary>
        /// <param name="imageLink"></param>
        /// <returns></returns>
        private TableCell WrapImageLink(ImageLink imageLink)
        {
            TableCell cell = new TableCell();
            cell.Controls.Add(imageLink);
            return cell;
        }



        /// <summary>
        /// Creates a table around the specified cell, and returns it
        /// </summary>
        /// <param name="mainCell"></param>
        /// <returns></returns>
        private Table CreateMainTable(TableCell mainCell)
        {
            Table mainTable = new Table();

            if (!String.IsNullOrEmpty(GalleryManager.MainTableClass))
                mainTable.CssClass = GalleryManager.MainTableClass;

            if (!String.IsNullOrEmpty(GalleryManager.MainCellClass))
                mainCell.CssClass = GalleryManager.MainCellClass;

            mainTable.Rows.Add(new TableRow());
            mainTable.Rows[0].Cells.Add(mainCell);

            return mainTable;
        }

        private string GetResourceUrl(string resourceName)
        {
            return this.Page.ClientScript.GetWebResourceUrl(this.GetType(),
                String.Format(CultureInfo.InvariantCulture,
                "{0}.{1}",
                Properties.Resources.ResourcesFolder,
                resourceName));
        }

        #endregion / Control builder methods /
    }
}
